<?php
namespace app\index\controller;

use think\Controller;
use app\index\model\Project;
use app\index\model\Archive;
use app\index\model\Carousel;
use app\admin\model\OrderContract;
use expand\BestSignDemo;
use think\Db;

class Index extends Common
{
    public function _initialize(){
        parent::_initialize();
    }
    
    public function tests(){
        $pModel = new Project();
        $dataList = $pModel->order('top desc,create_time desc')->limit(0,5)->select();
        dump($dataList);
    	die();
    }


    public function hey_test(){
        file_put_contents(date("Y-m-d H", time()) . 'test_charge.txt', 'hey_test' . PHP_EOL, FILE_APPEND);
        return 1;
    }

    public function index(Archive $aModel,Carousel $cmodel)
    {
        $parent = ['id' => '0'];
        $pModel = new Project();
        $where['sort'] = ['<',13];
        $dataList = $cmodel->selectInfo($where,'sort asc','*',8);
//        $dataList = $pModel->where($where)->order('create_time desc')->select();
//        $dataList->Team;
        $cwhere['status'] = ['eq',1];
        $cateList['list']['all'] = $aModel->selectLimitInfo($cwhere);
        $cwhere['typeid'] = ['eq',2];
        $cateList['list']['platform'] = $aModel->selectLimitInfo($cwhere);
        $cwhere['typeid'] = ['eq',3];
        $cateList['list']['news'] = $aModel->selectLimitInfo($cwhere);
        $cwhere['typeid'] = ['eq',1];
        $cateList['list']['project'] = $aModel->selectLimitInfo($cwhere);

        $this->assign('parent', $parent);
        $this->assign('BannerList', banners(3, 5)); //banner
        $this->assign('CateList', $cateList); //banner
        $this->assign('FinkList', flinks(1)); //合作伙伴
        $this->assign('dataList',$dataList);
        return $this->fetch();
    }
    
    public function newarc($page){
        $archive = new \app\index\model\Archive();
        $typeid = 1;
        $typeidStr = cache('ARCTYPE_ARR_'.$typeid);
        if (!$typeidStr){
            $arctype = new \app\index\model\Arctype();
            $arctype::$allChild = array();   //初始化无限子分类数组
            $typeidArr = $arctype->allChildArctype($typeid);
            $typeidStr = implode(',', $typeidArr);
            cache('ARCTYPE_ARR_'.$typeid, $typeidStr);
        }
        $where['status'] = 1;
        $where['typeid'] = ['in', $typeidStr];
        $dataList = $archive->where($where)->order('id desc')->page($page.', 5')->select();
        
        foreach ($dataList as $k => $val){
            $flag_arr = explode(',', $val['flag']);
            if(in_array('j',$flag_arr) && !empty($val['jumplink'])){
                $dataList[$k]['arcurl'] = $val['jumplink'];
            }else{
                $dataList[$k]['arcurl'] = url('detail/'.$val->arctype->dirs.'/'.$val['id']);
            }
        }
        
        $this->assign('dataList', $dataList);
        return $this->fetch('inc/new_arc');
    }
	
	public function section2(){
		$pModel = new Project();
		if(input('status')){
                    if(input('status') == 7){
                        $where['status'] = ['>',0];
                    }else{
                        $where['status'] = input('status') >= 3 ? ['>=', input('status')] : input('status');
                    }
		}else{
                    $where['status'] = ['>',0];
                    $where['top'] = 1;
		}
		$dataList = $pModel->where($where)->order('create_time desc')->select();
		$dataList->Team;
		$this->assign('dataList',$dataList);
		return $this->fetch();
	}
	
	public function section3(){
		$pModel = new Project();
		if(input('status')){
			$where['status'] = input('status');
		}else{
			$where['status'] = ['not in',[0,-2]];
			$where['top'] = 1;
		}
		$dataList = $pModel->where($where)->order('top desc,create_time desc')->select();
		$dataList->Team;
		$this->assign('dataList',$dataList);
		return $this->fetch();
	}

    public function lockcontract(){
        $ocModel = new OrderContract();
        $Best_data = new BestSignDemo();
        $list = $ocModel->where(['sign'=>3,'is_lock'=>0])->limit(500)->select();
        $list = empty($list)?array():$list->toArray();
        if(empty($list)){
            echo '数据更新完成';exit;
        }
        foreach($list as &$v){
            $res = $Best_data->lockContract($v['contract_id']);
            if($res['errno']==0 || $res['errno']==241423){
                $ocModel->where(['id'=>$v['id']])->update(['is_lock'=>1]);
            }
        }
    }

    public function aaaa()
    {
        //获取平台所有项目
        $pmodel = new \app\index\model\Project();
        $mmodel = new \app\index\model\Member();
        $project_list = $pmodel->select()->toArray();
        $arr2 = ['姓名','手机号'];
        foreach ($project_list as $key => $value) {
            $arr2[] = $value['project_name'];
        }
        $member_list = Db::query('select user_id from tf_b_gx where parent_id=11');
        $tableHeader = ['姓名','手机号','那些年的青春','三国之战神无双','仁心仁术','暗黑者','暗黑者第二期','缉妖法海传','暗黑者第三期','伏妖天师','《伏妖天师》','争分夺秒(第一期)','争分夺秒(第二期)','争分夺秒（第三期）','那座城，那个人','郊区的鸟','追·踪','那座城，那个人(第二期)','霍家拳之铁臂娇娃(第一期)','霍家拳之铁臂娇娃(第二期)','霍家拳之铁臂娇娃(第三期)','追踪','我们的科幻世界(第一期)','霍家拳之铁臂娇娃（第三期）','我们的科幻世界(第二期)','37°2——这座城，那个人(第三期)','被光抓走的人','被光抓走的人(第二期)','封神无双志'];

        foreach($member_list as $k=>$v){
            $member[$k]['username'] = 'null';
            $member[$k]['mobile'] = '1';
            $adata = $mmodel->field('username,mobile')->where(['id'=>$v['user_id']])->find()->toArray();
            if ($adata) {
                $member[$k]['username'] = $adata['username'];
                $member[$k]['mobile'] = $adata['mobile'];
            }
            $member[$k]['a'] = 0;
            $member[$k]['b'] = 0;
            $member[$k]['c'] = 0;
            $member[$k]['d'] = 0;
            $member[$k]['a1'] = 0;
            $member[$k]['a2'] = 0;
            $member[$k]['a3'] = 0;
            $member[$k]['a4'] = 0;
            $member[$k]['a5'] = 0;
            $member[$k]['a6'] = 0;
            $member[$k]['a7'] = 0;
            $member[$k]['a8'] = 0;
            $member[$k]['a9'] = 0;
            $member[$k]['a11'] = 0;
            $member[$k]['a12'] = 0;
            $member[$k]['a13'] = 0;
            $member[$k]['a14'] = 0;
            $member[$k]['a15'] = 0;
            $member[$k]['a16'] = 0;
            $member[$k]['a17'] = 0;
            $member[$k]['a18'] = 0;
            $member[$k]['a19'] = 0;
            $member[$k]['a21'] = 0;
            $member[$k]['a22'] = 0;
            $member[$k]['a23'] = 0;
            $member[$k]['a24'] = 0;
            $member[$k]['a25'] = 0;
            //获取用户投资明细
            $order = Db::query('select sum(a.money) total_money,a.project_id,b.project_name from tf_order as a left join tf_project as b on a.project_id=b.id where a.status=2 and uid='.$v['user_id'].' group by a.project_id');
            if (!empty($order)) {
                foreach ($order as $key=>$val) {
                    if ($val['project_id'] == '6') {
                        $member[$k]['a'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '9') {
                        $member[$k]['b'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '10') {
                        $member[$k]['c'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '11') {
                        $member[$k]['d'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '12') {
                        $member[$k]['a1'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '14') {
                        $member[$k]['a2'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '15') {
                        $member[$k]['a3'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '16') {
                        $member[$k]['a4'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '17') {
                        $member[$k]['a5'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '19') {
                        $member[$k]['a6'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '25') {
                        $member[$k]['a7'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '27') {
                        $member[$k]['a8'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '28') {
                        $member[$k]['a9'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '29') {
                        $member[$k]['a11'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '30') {
                        $member[$k]['a12'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '31') {
                        $member[$k]['a13'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '32') {
                        $member[$k]['a14'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '33') {
                        $member[$k]['a15'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '34') {
                        $member[$k]['a16'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '35') {
                        $member[$k]['a17'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '37') {
                        $member[$k]['a18'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '41') {
                        $member[$k]['a19'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '44') {
                        $member[$k]['a21'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '45') {
                        $member[$k]['a22'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '46') {
                        $member[$k]['a23'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '50') {
                        $member[$k]['a24'] = $val['total_money'];
                    }
                    if ($val['project_id'] == '51') {
                        $member[$k]['a25'] = $val['total_money'];
                    }
                }
            }



        }
        ToPHPEXECL($tableHeader,$member,'数据');
    }
}
